package com.example.b2t.thaicalendar;

import java.text.DateFormatSymbols;
import java.util.Calendar;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

public class B2TThaiCalendarActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_b2t_thai_calendar);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.row_header, new PlaceholderFragment())
                    .commit();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.b2_tthai_calendar, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_b2t_thai_calendar, container, false);
            drawCalendar(rootView, savedInstanceState);
            return rootView;
        }
        
        @SuppressLint("NewApi")
        private void drawCalendar(View rootView, Bundle savedInstanceState) {
            int numWeeks = 6;

            Calendar cal = Calendar.getInstance();
            int today = cal.get(Calendar.DAY_OF_YEAR);
            int todayYear = cal.get(Calendar.YEAR);
            int thisMonth = cal.get(Calendar.MONTH);
            
            cal.set(Calendar.DAY_OF_MONTH, 1);
            
            int todayDayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
            cal.add(Calendar.DAY_OF_MONTH, 1 - todayDayOfWeek);
            
            DateFormatSymbols dfs = DateFormatSymbols.getInstance();
            String[] weekdays = dfs.getShortWeekdays();        
            
            LinearLayout headerLayout = (LinearLayout) rootView.findViewById(R.id.row_header);
            headerLayout.removeAllViews();
            
            for (int day = Calendar.SUNDAY; day <= Calendar.SATURDAY; day++) {
            	TextView headerTextView = new TextView(getActivity());
            	headerTextView.setTextAppearance(getActivity(), android.R.attr.textAppearanceLarge);
            	headerTextView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT,1f));
            	headerTextView.setText(weekdays[day]);
            	headerLayout.addView(headerTextView);
            }
            
            
            LinearLayout weekLayout = (LinearLayout) rootView.findViewById(R.id.row_week);
            weekLayout.removeAllViews();
            
            for (int week = 0; week < numWeeks; week++) {
            	LinearLayout dayLayout = new LinearLayout(getActivity());
            	dayLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT,1f));
                for (int day = 0; day < 7; day++) {
                    boolean inMonth = cal.get(Calendar.MONTH) == thisMonth;
                    boolean inYear  = cal.get(Calendar.YEAR) == todayYear;
                    boolean isToday = inYear && inMonth && (cal.get(Calendar.DAY_OF_YEAR) == today);
    
                    boolean isFirstOfMonth = cal.get(Calendar.DAY_OF_MONTH) == 1;
                    
                    TextView dayTextView = new TextView(getActivity());
                    dayTextView.setTextAppearance(getActivity(), android.R.attr.textAppearanceLarge);
                    dayTextView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT,1f));
                   
	                if(inMonth){
	                	dayTextView.setText(Integer.toString(cal.get(Calendar.DAY_OF_MONTH)));
                    }else{
                    	dayTextView.setText("");
                    }
	                dayLayout.addView(dayTextView);
                    cal.add(Calendar.DAY_OF_MONTH, 1);
                }
                weekLayout.addView(dayLayout);
            }
        }
    }
    
}
